package com.laputa.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.EventDispatchAction;

import com.laputa.service.method.RegisterService;

public class RegisterAction extends EventDispatchAction
{
	private RegisterService registerService;

	/*
	 * Generated Methods
	 */
	public void setRegisterService(RegisterService registerService){
		this.registerService = registerService;
	}
	
	public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	{
		DynaActionForm registerForm = (DynaActionForm) form;
		String name = (String)registerForm.get("name");
		String password = (String)registerForm.get("password");
		String confirmPwd = (String)registerForm.get("confirmpwd");
		String email = (String)registerForm.get("email");
		
		String errorMsg = registerService.validate(name, password, confirmPwd);
		System.out.println("errorMSG = " + errorMsg);
		if (errorMsg != null) {
			request.setAttribute("errorMsg", errorMsg);
			return mapping.findForward("fail");
		}
		
		registerService.save(name, password, email);
		return mapping.findForward("login");
	}

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	{
		return null;
	}
}
